﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.OpenGL;
using Silvermoon.Utils;
using Silvermoon.UI;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using Silvermoon.Images;
using System.Globalization;

namespace Testing
{
    public class PickerDemo : DemoWindowBase
    {
        private static bool autoHide = false;
        private static int dayIndex = 15;
        private static int weekIndex = 3;

        private bool show = false;
        private Picker slotDayNumberPicker;
        private Picker slotWeekNamePicker;
        private Picker dayNumberPicker;
        private Picker dayOfWeekPicker;
        private FontSprite font;

        private static readonly string[] daysOfWeek = CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames;

        private static readonly int[] days = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };



        public static PropertyAccessor<PickerDemo, float> PickerTransitionProperty = PropertyAccessor.Register<PickerDemo, float>(
            "PickerTransition",
            c => c.PickerTransition,
            (c, v) => c.PickerTransition = v);

        public PickerDemo()
            : base()
        {
            Initialize();
        }

        protected override void Dispose(bool dispose)
        {
            base.Dispose(dispose);
            SlotMachineItem.DisposeItems();
        }

        public static bool AutoHide;

        public float PickerTransition
        {
            get { return dayNumberPicker.SiblingTransition; }
            set
            {
                dayNumberPicker.SiblingTransition = value;
                dayOfWeekPicker.SiblingTransition = value;
            }
        }

        #region initialization

        private void Initialize()
        {
            //SetTransition(300, TransitionMask.RotateCenterY, TransitionMask.RotateCenterY);
            SetTransition(300, TransitionMask.Zoom | TransitionMask.Fade, TransitionMask.RotateCenterY);
            font = FontSprite.FromData(GlFontFamily.GenericSansSerif, 16, FontStyle.Regular);
            Color = PhoneColors.MediumDark;

            GridDefinition def = new GridDefinition();
            def.Rows.AddRange(1f, 1f);
            def.Columns.AddRange(1f);
            def.CreateAutoCells();
            AnimationGrid g = new AnimationGrid { Margin = Margin.Zero, Definition = def };

            Panel slot = CreateSlotMachine(false);
            slot.Margin = new Margin(24, 0, 24, 24);
            slotDayNumberPicker = dayNumberPicker;
            slotWeekNamePicker = dayOfWeekPicker;

            Panel auto = CreateSlotMachine(true);
            auto.Margin = new Margin(24, 24, 24, 0);

            g.AddControl(1, slot);
            g.AddControl(0, auto);
            Controls.Add(g);
            InitializeToolbar();
        }


        private Panel CreateSlotMachine(bool auto)
        {
            Panel container = new Panel();
            //p.Chrome = new BackgroundShape { Texture = SysImages, Style = RenderStyle.Edged };

            dayNumberPicker = new Picker { Margin = new Margin(284, 0, 0, 0), IsCyclic = true, Mode = PickerMode.Fade, AutoHide = auto };
            dayOfWeekPicker = new Picker { Margin = new Margin(0, 0, null, 0), Width = auto ? 286 : 280, IsCyclic = false, AutoHide = auto };

            dayNumberPicker.GetVirtualItem += GetSlotMachineItem;
            dayOfWeekPicker.GetVirtualItem += GetSlotMachineItem;


            if (!auto)
            {
                container.Chrome = new BorderShape(3, Color.Empty);
                container.ChromeColor = PhoneColors.FromArgb(64, 0, 0, 0);

                dayNumberPicker.Background = new PickerShape(Color.White);
                dayOfWeekPicker.Background = new PickerShape(Color.White);

                dayNumberPicker.TextColor = PhoneColors.Accent;
                dayOfWeekPicker.TextColor = PhoneColors.Orange;
            }
            else
            {
                //dayNumberPicker.TextColor = PhoneColors.White;
                //dayOfWeekPicker.TextColor = PhoneColors.White;
                //dayNumberPicker.UnselectedTextColor = PhoneColors.Medium;
                //dayOfWeekPicker.UnselectedTextColor = PhoneColors.Medium;

                dayNumberPicker.Lense = new BorderShape(5, Color.White);
                dayOfWeekPicker.Lense = new BorderShape(5, Color.White);
            }


            dayNumberPicker.SelectedItemChanged += (s, e) =>
            {
                dayIndex = dayNumberPicker.SelectedItemIndex;
            };
            dayOfWeekPicker.SelectedItemChanged += (s, e) => weekIndex = dayOfWeekPicker.SelectedItemIndex;

            CreateNodes();
            dayNumberPicker.SelectedItemIndex = dayIndex;
            dayOfWeekPicker.SelectedItemIndex = weekIndex;
            container.Controls.Add(dayNumberPicker, dayOfWeekPicker);
            return container;
        }

        void GetSlotMachineItem(object sender, PickerItemEventArgs e)
        {
            var item = SlotMachineItem.GetItem();
            item.Margin = new Margin(16, 4, 16, 4);
            item.Text = e.Value.ToString();
            item.Font = font;
            e.Container = item;
        }

        private void CreateNodes()
        {
            dayNumberPicker.ItemsSource = days;
            dayOfWeekPicker.ItemsSource = daysOfWeek;

        }

        private void InitializeToolbar()
        {
            Toolbar tb = this.toolbar;
            tb.Buttons.Clear();
            Button cancelButton = new Button { Content = "Cancel", Size = new Size(140, 64) };
            Button okButton = new Button { Content = "OK", Size = new Size(140, 64) };
            tb.IsMenuButtonVisible = true;
            okButton.Tap += (s, e) => GoBack();
            //cancelButton.Tap += (s, e) => Animate(); // Hide();
            cancelButton.Tap += (s, e) => slotDayNumberPicker.SetItemIndex(3, true);
            tb.Buttons.Add(okButton);
            tb.Buttons.Add(cancelButton);

            MenuItem item = new MenuItem("Auto Hide") { Height = 96 };
            item.Tap += (s, e) => ToggleAutoHide();
            tb.MenuItems.Add(item);
        }

        #endregion

        private void ToggleAutoHide()
        {
            autoHide ^= true;
            dayOfWeekPicker.AutoHide = autoHide;
            dayNumberPicker.AutoHide = autoHide;
        }

        private void Animate()
        {
            FloatTransition transition = new FloatTransition
            {
                Duration = 500,
                To = show ? 1f : 0f,
                Tracking = TrackMode.Keep
            };
            show ^= true;
            PickerTransitionProperty.BeginAnimation(this, transition);
        }

        protected override Shape CreateDefaultBackground()
        {
            return PhoneColors.Dark;
        }

        protected override Shape CreateToolbarBackground()
        {
            return Color.Black;
        }



    }
}
